﻿namespace SwyishChess.Domain.Player
{
    public class ChessPlayersModel
    {
        private const int ONE_PLAYER_MODE = 1;
        private const int TWO_PLAYER_MODE = 2;
        private const int MAX_CHESS_PLAYERS_COUNT = 2;
        private const int PLAYER_ONE = 1;
        private const int PLAYER_TWO = 2;
        private const int BLACK_PLAYER = 1;
        private const int WHITE_PLAYER = 0;

        /// <summary>
        /// Represents chess players in the game
        /// </summary>
        public Player[] ChessPlayers { get; set; }
        /// <summary>
        /// Represents the current turn
        /// </summary>
        public Player PlayerTurn { get; set; }

        /// <summary>
        /// Overloaded constructor which creates players based on the input from the user
        /// </summary>
        /// <param name="humanPlayerCount">The number of human players in the game</param>
        public ChessPlayersModel(int humanPlayerCount)
        {
            ChessPlayers = new Player[MAX_CHESS_PLAYERS_COUNT];

            if (humanPlayerCount == TWO_PLAYER_MODE)
            {
                ChessPlayers[WHITE_PLAYER] = new HumanPlayer(PLAYER_ONE, Color.WHITE);
                ChessPlayers[BLACK_PLAYER] = new HumanPlayer(PLAYER_TWO, Color.BLACK);
            }

            // Sets the default turn to white player
            PlayerTurn = ChessPlayers[WHITE_PLAYER];
        }

        /// <summary>
        /// To change the current turn to the opposite player
        /// </summary>
        public void ChangePlayerTurn()
        {
            if (PlayerTurn == ChessPlayers[BLACK_PLAYER])
            {
                PlayerTurn = ChessPlayers[WHITE_PLAYER];
            }
            else if (PlayerTurn == ChessPlayers[WHITE_PLAYER])
            {
                PlayerTurn = ChessPlayers[BLACK_PLAYER];
            }
        }
    }
}
